package problem.year2021.january;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class January21 {
    class Solution {
        public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {
            int m = edges.length;
            int[][] newEdges = new int[m][4];
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < 3; ++j) {
                    newEdges[i][j] = edges[i][j];
                }
                newEdges[i][3] = i;
            }
            Arrays.sort(newEdges, new Comparator<int[]>() {
                public int compare(int[] u, int[] v) {
                    return u[2] - v[2];
                }
            });

            // 计算 value
            UnionFind ufStd = new UnionFind(n);
            int value = 0;
            for (int i = 0; i < m; ++i) {
                if (ufStd.unite(newEdges[i][0], newEdges[i][1])) {
                    value += newEdges[i][2];
                }
            }

            List<List<Integer>> ans = new ArrayList<List<Integer>>();
            for (int i = 0; i < 2; ++i) {
                ans.add(new ArrayList<Integer>());
            }

            for (int i = 0; i < m; ++i) {
                // 判断是否是关键边
                UnionFind uf = new UnionFind(n);
                int v = 0;
                for (int j = 0; j < m; ++j) {
                    if (i != j && uf.unite(newEdges[j][0], newEdges[j][1])) {
                        v += newEdges[j][2];
                    }
                }
                if (uf.setCount != 1 || (uf.setCount == 1 && v > value)) {
                    ans.get(0).add(newEdges[i][3]);
                    continue;
                }

                // 判断是否是伪关键边
                uf = new UnionFind(n);
                uf.unite(newEdges[i][0], newEdges[i][1]);
                v = newEdges[i][2];
                for (int j = 0; j < m; ++j) {
                    if (i != j && uf.unite(newEdges[j][0], newEdges[j][1])) {
                        v += newEdges[j][2];
                    }
                }
                if (v == value) {
                    ans.get(1).add(newEdges[i][3]);
                }
            }

            return ans;
        }
    }

    // 并查集模板
    class UnionFind {
        int[] parent;
        int[] size;
        int n;
        // 当前连通分量数目
        int setCount;

        public UnionFind(int n) {
            this.n = n;
            this.setCount = n;
            this.parent = new int[n];
            this.size = new int[n];
            Arrays.fill(size, 1);
            for (int i = 0; i < n; ++i) {
                parent[i] = i;
            }
        }

        public int findset(int x) {
            return parent[x] == x ? x : (parent[x] = findset(parent[x]));
        }

        public boolean unite(int x, int y) {
            x = findset(x);
            y = findset(y);
            if (x == y) {
                return false;
            }
            if (size[x] < size[y]) {
                int temp = x;
                x = y;
                y = temp;
            }
            parent[y] = x;
            size[x] += size[y];
            --setCount;
            return true;
        }

        public boolean connected(int x, int y) {
            x = findset(x);
            y = findset(y);
            return x == y;
        }
    }
}
